package com.zhongke.DKYCboot.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class AllUtils {


    /**
     * 判断参数是否为null
     *
     * @param o
     * @return
     */
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if ((o instanceof String)) {
            if (((String) o).trim().length() == 0) {
                return true;
            }
        } else if ((o instanceof Collection)) {
            if (((Collection) o).isEmpty()) {
                return true;
            }
        } else if (o.getClass().isArray()) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (((o instanceof Map)) && (((Map) o).isEmpty())) {
            return true;
        }

        return false;
    }

    /**
     * 判断是否不为空
     * @param o
     * @return
     */
    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }
    /**
     * ZTD 2019/03/06
     * 将String[]转换成list集合
     * @param all
     * @return
     */
    public final static List<String> changeList(String all){
        List<String> list= Arrays.asList(all.split(","));
        return list;

    }
    /**
     * ZTD 2019/03/04
     * 生成Token的唯一标识码
     *
     * @return
     */
    public final static UUID getToken() {
        //生成token的值
        UUID token = UUID.randomUUID();
        return token;
    }

    /**
     * @param num1
     * @param num2
     * @return
     * @author:张天德
     * @date: 2019年3月19日 下午1:11:00
     * @Description:数据的商业化算法 乘法
     */
    public static double multiply(double num1, double num2) {
        BigDecimal bd = new BigDecimal(Double.toString(num1));
        BigDecimal bd2 = new BigDecimal(num2);
        return bd.multiply(bd2).doubleValue();
    }

    /**
     * @param one
     * @param two
     * @return
     * @author:Liqingchao
     * @date: 2019年3月19日 下午1:12:10
     * @Description: 数据的商业化算法 加法
     */
    public static double add(double one, double two) {
        BigDecimal bd = new BigDecimal(Double.toString(one));
        BigDecimal bd2 = new BigDecimal(Double.toString(two));
        return bd.add(bd2).doubleValue();

    }

    /**
     * @param one
     * @param two
     * @return
     * @author:Liqingchao
     * @date: 2019年3月19日 下午1:13:40
     * @Description: 数据的商业化算法 减法
     */
    public static double subtract(double one, double two) {
        BigDecimal bd = new BigDecimal(Double.toString(one));
        BigDecimal bd2 = new BigDecimal(Double.toString(two));
        return bd.subtract(bd2).doubleValue();

    }

    /**
     * @param one
     * @param two
     * @return
     * @author:Liqingchao
     * @date: 2019年3月19日 下午1:15:08
     * @Description: 数据的商业化算法 除法
     */
    public static double divide(double one, double two) {
        BigDecimal bd = new BigDecimal(Double.toString(one));
        BigDecimal bd2 = new BigDecimal(Double.toString(two));
        return bd.divide(bd2, 2).doubleValue();

    }

    /**
     * @return
     * @author Liqingchao
     * @Date 11:14 2020/5/13
     * 获取token
     */
    public static String getUserToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token;
        if(request.getParameter("token").equals("null") || isEmpty(request.getParameter("token"))){
            token=null;
        }else{
            token = request.getParameter("token");
        }
        return token;
    }
    /**
     * ZTD 2019/03/06
     * 将List集合转换成String字符串
     * @param list
     * @return
     */
    public  static final String changeString(List<String> list) {
        String string= String.join(",", list);
        return string;
    }

    /**
     * @param source
     * @param type
     * @param <T>
     * @return
     * @author Liqingchao
     * @Date 11:15 2020/5/13
     * 拷贝集合
     */
    public static <T> List<T> copyBeanList(List<?> source, Class<T> type) {
        List<T> target = new ArrayList<>();
        try {
            for (Object obj : source) {
                T t = type.newInstance();
                BeanUtils.copyProperties(obj, t);
                target.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return target;
    }
    /**
      *@author Liqingchao
      *@Date 11:47 2020/5/14
      *@Param FileList folder
      *@return
      * 上传单张图片
      */
    public static String pictureUp(MultipartFile FileList, String folder) {
        String url = null;
        InputStream inputStream = null;
        try {
            OssUtil ossUtil = new OssUtil();
            inputStream = FileList.getInputStream();
            String filename = FileList.getOriginalFilename();
            url = ossUtil.uploadImageToOSS(filename, inputStream, folder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }





    /**
     * 小时换算成毫秒
     * @param numTime
     * @return
     */
    public int hms (Double numTime){
        int ms=new Double(numTime*36000).intValue();
        return  ms;
    }




    /**
     * 所有类型转换成 base64 redis 使用
     * @param obj
     * @return
     */
    public static String  base64(Object obj){
        return  Base64.encodeBase64String(JSON.toJSONString(obj).getBytes());
    }
    public static Map<String,Object> baser64ToMap(String s){
        return  JSON.parseObject(new String(Base64.decodeBase64(s)), Map.class);
    }
    public static  List baser64ToList(String s){
        return  JSON.parseObject(new String(Base64.decodeBase64(s)), List.class);
    }

    /**
     * 获取当前时间前30天的日期
     * @return
     */
  /*  public static  Date getTime30(){
        Date now = new Date();
        Date endDate = DateUtils.addDays(now, -30);
         return endDate;
    }*/
    /**
     * 将时间转换为时间戳
     *
     * @param date
     */
    public static Long dateToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
        String tiem = sformat.format(date);
        return dateToStamp(tiem);
    }
    /**
     * @param s
     */
    public static Long dateToStamp(String s) {
        long ts = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = simpleDateFormat.parse(s);
             ts = date.getTime() / 1000;

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ts;
    }

    /**
     * Object 转换Date
     * @param o
     * @return
     */
    public static Date ObjectToDate(Object o){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
        Date d=null;
        try {
             d=format.parse(o.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  d;
    }
}


